package cn.nexd.app.collector.util;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ImageView;

/**
 * Created by xun on 2016/7/27.
 */
public class AnimUtil {
    public static ObjectAnimator splashAnim(View view, float finalAlpha, float finalY) {
        PropertyValuesHolder translateAnim = PropertyValuesHolder.ofFloat("translationY", finalY);
        PropertyValuesHolder alphaAnim = PropertyValuesHolder.ofFloat("alpha", 0, finalAlpha);
        ObjectAnimator viewAnim = ObjectAnimator.ofPropertyValuesHolder(view, alphaAnim, translateAnim);
        viewAnim.setDuration(1500);
        return viewAnim;
    }

    public static ValueAnimator foldAnim(final View layout, float[] degree) {
        layout.setVisibility(View.VISIBLE);
        layout.setPivotY(90);
        layout.setPivotX(0);
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(degree[0], degree[1]);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                layout.setRotationX((Float) animation.getAnimatedValue());
                layout.setAlpha(255 * animation.getAnimatedFraction());
                layout.getLayoutParams().height = (int) (320 * animation.getAnimatedFraction());
                layout.requestLayout();
            }
        });
        valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        valueAnimator.setDuration(600);
        valueAnimator.start();
        return valueAnimator;

    }

    public static ValueAnimator unFoldAnim(final View layout, float[] degree) {
        layout.setVisibility(View.VISIBLE);
        layout.setPivotY(90);
        layout.setPivotX(90);
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(degree[0], degree[1]);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                layout.setRotationX((Float) animation.getAnimatedValue());
                layout.setAlpha(255 - 255 * animation.getAnimatedFraction());
                layout.getLayoutParams().height = (int) (320 - 320 * animation.getAnimatedFraction());
                layout.requestLayout();
            }
        });
        valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        valueAnimator.setDuration(600);
        valueAnimator.start();
        return valueAnimator;

    }
}
